package fzu.functiongather.QRCode_Creat;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;

import fzu.functiongather.QRCode.QRCodeActivity;
import fzu.functiongather.R;
import fzu.functiongather.Utils.QRCodeUtil;

/**
 * Created by Administrator on 2016/8/17.
 */
public class QRCode_Creat_Activity extends Activity {
    private ImageView iv_qrcode;
    private String address="hello";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrcode_creat_main);
        initView();
    }
    private void initView(){
        iv_qrcode=(ImageView)findViewById(R.id.iv_qrcode);
        creatCode(address);
    }
    private void creatCode(final String url){
        Log.e("curl", url);
        final String filePath = getFileRoot(QRCode_Creat_Activity.this) + File.separator
                + "qr_" + System.currentTimeMillis() + ".jpg";
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean success = QRCodeUtil.createQRImage(url.trim(), 800, 800, null, filePath);
              /*  BitmapFactory.decodeResource(getResources(), R.mipmap.ailou);*/ //设置二维码中心图标F
                if(success){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("maddress",url);
                            Log.e("qrpicture","run");
                            iv_qrcode.setImageBitmap(BitmapFactory.decodeFile(filePath));
                        }
                    });

                }

            }
        }).start();
    }
    private String getFileRoot(Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File external = context.getExternalFilesDir(null);
            if (external != null) {
                return external.getAbsolutePath();
            }
        }

        return context.getFilesDir().getAbsolutePath();
    }
}
